import request from '@/utils/request'
// import MenuItem from 'element-plus/es/components/menu/src/utils/menu-item.mjs'
type Common<T> = {
    code: string
    data: T
    mesg: string
    time: string
}

export type MenuItem = {
    createdBy: string
    createdTime: string
    description: string
    href: string
    icon: string
    id: number
    level: number
    name: string
    operatorId: number | null
    orderNum: number
    parentId: number
    shown: boolean
    updatedBy: string
    updatedTime: string
}


// 定义创建菜单或编辑的所需类型 1.partial(全部)   2.pick(挑选)   3.Omit(排除)
// export type CreateOrEditMenu = Partial<menuItem>
export type CreateOrEditMenu = Pick<MenuItem, "name" | "href" | "parentId" | "description" | "icon" | "shown" | "orderNum"> & { id?: number }
// export type MenuInfo = Omit<menuItem, "createdBy" | "createdTime" | "operatorId" | "updatedBy" | "updatedTime">


// 获取所有菜单逻辑
export const getAll = () => {
    return request<Common<MenuItem[]>>({
        method: 'GET',
        url: '/boss/menu/getAll',
    })
}


// 创建或编辑菜单逻辑
export const saveOrUpdate = (MenuInfo: CreateOrEditMenu) => {
    return request<Common<boolean>>({
        method: 'POST',
        url: '/boss/menu/saveOrUpdate',
        data: MenuInfo,  //传递菜单信息
    })
}


// 删除菜单的逻辑
export const deleteMenu = (id: MenuItem['id']) => {
    return request<Common<boolean>>({
        method: 'DELETE',
        url: `/boss/menu/${id}`
    })
}


// 定义类型
type subMenuItem = MenuItem & {
    subMenuList: subMenuItem[] | null
}
type EditMenuInfo = Common<{ menuInfo: MenuItem, parentMenuList: subMenuItem[] }>
//获取指定的菜单详情
export const getEditMenuInfo = (id: number) => {
    return request<EditMenuInfo>({
        method: 'GET',
        url: `/boss/menu/getEditMenuInfo`,
        params: {
            id
        }
    })
}